import java.util.Random;
import java.util.Scanner;

public class HW_3_miGong {
    public static void main(String[] args) {
        System.out.println("-----迷宫游戏-----");
        int[][] arr = new int[5][5];
        Random r=new Random();
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                int x=r.nextInt(2);
                arr[i][j]=x;
            }
        }
        arr[0][0]=2;
        arr[4][4]=0;
        printStr(arr);
        while(true){
            if (arr[4][4]==2){
                System.out.println("恭喜你成功走出迷宫了");
                break;
            }
            move(arr);
            printStr(arr);
        }
    }
    public static void printStr(int[][] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
    public static void move(int[][] arr) {
        int hang=0,lie=0;
        Scanner sc=new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (arr[i][j]==2) {
                    hang=i;
                    lie=j;
                    break;
                }
            }
        }
        System.out.println("请输入w(上),a(左),s(下),d(右)进行移动");
        String w=sc.nextLine();
        if (w.equals("w")&&(hang-1)>=0&&arr[hang-1][lie]!=1){
           arr[hang][lie]=0;
           arr[hang-1][lie]=2;
        } else if (w.equals("a")&&(lie-1)>=0&&arr[hang][lie-1]!=1) {
            arr[hang][lie]=0;
            arr[hang][lie-1]=2;
        } else if (w.equals("s")&&(hang+1)<=4&&arr[hang+1][lie]!=1) {
            arr[hang][lie]=0;
            arr[hang+1][lie]=2;
        } else if (w.equals("d")&&(lie+1)<=4&&arr[hang][lie+1]!=1) {
            arr[hang][lie]=0;
            arr[hang][lie+1]=2;
        } else {System.out.println("撞墙了或输入错误,请重试");}
        return;
    }
}
